var channel_active = false;
var channel
var socket

function init_channel(token) {
	console.log("INIT");
    channel = new goog.appengine.Channel(token);
    socket = channel.open();
    socket.onopen = onOpened;
    socket.onmessage = onMessage;
    socket.onerror = onError;
    socket.onclose = onClose;
    console.log("READY TO GO");
}

function request_map() {
	$.post("/requestmap");
}

function onOpened() {
	channel_active = true;
	console.log("OPENED");
	$.post('/channelready');
}
function onMessage(message) {
	if (message.data == "SERVER ACK") {
		console.log(message.data);
	} else if (message.data == "GET MAP"){
		var map_resources = [];
		_.each(resource_names, function(node_name){
			if ($("#checkArea_" + node_name).prop("checked")) {
				map_resources.push(node_name);
			}
		});
		var address = $("#simulator_address").val();
		$.post("getnexttick", 
				{"tick": tick, 'resources': map_resources, 'css': true, 'address': address},
				animate_second, 'json');
	}
}
function onError() {
	console.log("ERROR");
}
function onClose() {
	console.log("CLOSE");
}

